﻿cmake_minimum_required(VERSION 3.14)

project(Serial_tools LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_SYSTEM_VERSION 10.0)

########## WDK Header Support ##########
set(WDK_ROOT "C:/Program Files/Windows Kits/10/")
set(WDK_VERSION "10.0.26100.0")

include_directories(
    "${WDK_ROOT}/Include/${WDK_VERSION}/km"
    "${WDK_ROOT}/Include/${WDK_VERSION}/um"
    "${WDK_ROOT}/Include/${WDK_VERSION}/shared"
)
########## END Support ##########

# Qt Support
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Gui Core Widgets SerialPort Network)

add_executable(Serial_tools
  main.cpp
  ui/MainWindow.h
  ui/MainWindow.cpp
  ui/MainWindow.ui
  ui/CustomTreeStyle.h
  ui/CustomTreeStyle.cpp
  ui/EndPointListWidget.h
  ui/EndPointListWidget.cpp
  resources.qrc

  core/DataFormatter.h core/DataFormatter.cpp
  core/DriverManager.h core/DriverManager.cpp
  core/ServiceManager.h core/ServiceManager.cpp
  core/DriverController.h core/DriverController.cpp

  core/C0C_Def.h
  core/EnumDevices.h core/EnumDevices.cpp
  core/DriverStruct.h core/DriverStruct.cpp
  core/PortNum.h core/PortNum.cpp
  core/Params.h core/Params.cpp
  core/ComDb.h core/ComDb.cpp
  core/Utils.h core/Utils.cpp
  core/InfFile.h core/InfFile.cpp

  core/control/DataControl.h core/control/DataControl.cpp
)

target_link_libraries(Serial_tools 
  Qt${QT_VERSION_MAJOR}::Core
  Qt${QT_VERSION_MAJOR}::Gui
  Qt${QT_VERSION_MAJOR}::Widgets
  Qt${QT_VERSION_MAJOR}::SerialPort
  Qt${QT_VERSION_MAJOR}::Network
)

########## WDK Libs Support ##########
link_directories(
    "${WDK_ROOT}/Lib/${WDK_VERSION}/um/x64"
    "${WDK_ROOT}/Lib/${WDK_VERSION}/km/x64"
)

target_link_libraries(Serial_tools
  setupapi
  msports
  shlwapi
  newdev
)
########## END Support ##########

# Add include directories so headers in subfolders can be found
target_include_directories(Serial_tools
  PRIVATE
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/core
    ${CMAKE_SOURCE_DIR}/ui
)

# Force MSVC to interpret source files as UTF-8 to avoid C4819 warnings
if (MSVC)
  target_compile_options(Serial_tools PRIVATE /utf-8)
  # 通过链接器选项嵌入 UAC 配置
  target_link_options(Serial_tools PRIVATE
    "/MANIFESTUAC:level='requireAdministrator' uiAccess='false'"
  )
endif()

# 在配置阶段将 sys 中的驱动文件复制到构建目录（支持通配符）
file(COPY ${CMAKE_SOURCE_DIR}/sys/
     DESTINATION ${CMAKE_BINARY_DIR}/sys/
     FILES_MATCHING
       PATTERN "*.inf"
       PATTERN "*.sys"
       PATTERN "*.cat")

include(GNUInstallDirs)
install(TARGETS Serial_tools
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# 安装时将 sys 中的驱动文件复制到可执行文件目录
install(
  DIRECTORY ${CMAKE_SOURCE_DIR}/sys/
  DESTINATION ${CMAKE_INSTALL_BINDIR}
  FILES_MATCHING
    PATTERN "*.inf"
    PATTERN "*.sys"
    PATTERN "*.cat"
)
